import Vue from 'vue'
import Vuex from 'vuex'

// 安装Vuex插件
Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        flag:true,
        isflag:true,
        isflag2:true,
        list:[],
        piece:0,
        num:0,
        username:''

    },
    mutations: {
        username(state,name){
            state.flag=!state.flag
            state.username=name
            console.log(state.username)
        },
        getisflag(state,boo){
            console.log(boo)
            state.isflag=boo;
        },
        getisflag2(state,bool){
            console.log(bool)
            state.isflag2=bool;
        },
        add(state,arr){
            console.log(arr)
        
           arr.num++;
            let newlist=[...state.list]
            let flag=false
            newlist.forEach((items,index)=>{
              
                if(items.id==arr.id){
                  items.num++;
                    flag=true
                }
            })

            if(!flag){
                newlist.push(arr)
            }
            state.list=newlist
        },
        del(state,item){
          console.log(item)
          console.log(state.list)
          let newlist=[...state.list]
             console.log(newlist)
          newlist.forEach((items,index)=>{
            // console.log(index)
              if(items.id==item.id){

                  console.log(items.id,item.id)
                 state.list.splice(items,1);
                 
              }
          })
      
      
         
        },
    
        addNum(state,item){
               item.num++;
           
        },
        reduce(state,item){ 
        
             if(item.num>1){
                  item.num--;
             }else if(item.num==1){
                this.commit('del',item)
          }
        
               
        }
    }
})
